package main

import (
	"fmt"
	"github.com/cmajor/cherry/cnet"
	"github.com/cmajor/cherry/iface"
	"github.com/cmajor/cherry/utils"
)

func main() {
	utils.GlobalConfig.Load("cherry.json")

	//todo 读取配置
	notification := make(chan *iface.InternalMsg, 1)
	s1 := &cnet.TCPServer{}
	s1.Init("app", "127.0.0.1", "11211", notification)

	go func() {

		for msg := range notification {
			//fmt.Println("notification msg id = ", msg.SysID)

			if msg.SysID < 0 {
				return
			}

			switch msg.SysID {
			case iface.NOTIFY_TCP_ESTABLISH:
				fmt.Println("conn ", msg.CustomData, "establish")
			case iface.NOTIFY_TCP_CLOSE:
				fmt.Println("conn ", msg.CustomData, "close")
			case iface.NOTIFY_TCP_READ:
				fmt.Println("conn id = ",msg.ConnID," read ", string(msg.Content))
				mecho := &iface.InternalMsg{SysID: iface.COMMAND_WRITE, Content: []byte("hello cherry client"), ConnID: msg.ConnID}
				s1.SendMsg(mecho)
			case iface.NOTIFY_TCP_WRITE:
				fmt.Println("conn ", msg.CustomData, "write")
			}
		}
		fmt.Println("notification exit")
	}()

	s1.Start()
}
